﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 拆分台车
    /// </summary>
    public class SplitCarrierService : BaseService<BaseEntity>, ISplitCarrierService
    {
        private readonly ISplitCarrierRepository repository;
        public SplitCarrierService(ISplitCarrierRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        public SetObjectDetail GetCarrierByName(string srcRackNo)
        {

            SetObjectDetail rfInfo = new SetObjectDetail();
            //查询台车信息
            List<WmsRackPackage> wmsRacks = repository.GetCarrierByName(srcRackNo);

            if (wmsRacks.Count == 0)
            {
                //提示  [A1-2-2]:该库位不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + srcRackNo + "]:该台车不存在，请检查!";
                return rfInfo;
            }
            else if (wmsRacks[0].Enabled != "Y")
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + srcRackNo + "]:该台车不可用，请检查!";
                return rfInfo;
            }

            rfInfo.type = "PASS";
            rfInfo.message = "[" + srcRackNo + "]:扫描成功,请扫描产品条码";
            rfInfo.wmsObjectDetail = wmsRacks.ConvertAll(s => (object)s);
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonNo(string srcRackNo, string cartonNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetSysStockBycartonNo(cartonNo);

            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            else if (string.IsNullOrEmpty(sysStocks[0].PartNo))
            {
                //提示  [M0090105]:该条码是NC隔离状态，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码对应零件号为空，请检查!";
                return rfInfo;
            }

            List<WmsRackPackage> wmsRacks = repository.GetCarrierByPartNo(sysStocks[0].PartNo);
            if (wmsRacks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码对应零件号未维护到台车，请检查!";
                return rfInfo;
            }
            List<WmsRackPackage> wmsRackPackages = repository.GetCarrierByRackAndPartNo(srcRackNo, sysStocks[0].PartNo);
            if (wmsRackPackages.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + cartonNo + "]:扫描成功,请输入台车容量";
                rfInfo.wmsObjectDetail = wmsRackPackages.ConvertAll(s => (object)s);
                return rfInfo;
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + srcRackNo + "]:该台车未维护此条码对应零件，请检查!";
                return rfInfo;
            }

        }

        public SetObjectDetail CheckStockByCartonNoAndQty(string srcRackNo, string cartonNo, string qty, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            List<SysStock> sysStocks = repository.GetSysStockBycartonNo(cartonNo);

            List<WmsRackPackage> wmsRackPackages = repository.GetCarrierByRackAndPartNo(srcRackNo, sysStocks[0].PartNo);
            if (wmsRackPackages.Count > 0)
            {
                if (Convert.ToInt32(qty) > Convert.ToInt32(wmsRackPackages[0].RackNum))
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "台车容量大于台车维护数量，请检查!";
                    return rfInfo;
                }
            }

            rfInfo.type = "PASS";
            rfInfo.message = "台车容量处理成功，请扫描目标台车";
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonNoAndQtyAddRackNo(string srcRackNo, string cartonNo, string qty, string destRackNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            List<SysStock> sysStocks = repository.GetSysStockBycartonNo(cartonNo);

            List<WmsRackPackage> wmsRackPackages = repository.GetCarrierByRackAndPartNo(srcRackNo, sysStocks[0].PartNo);
            if (wmsRackPackages.Count > 0)
            {
                if (Convert.ToInt32(qty) <= Convert.ToInt32(wmsRackPackages[0].RackNum))
                {
                    //新增料架与包装信息
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                                         ([rack_no]
                                         ,[corton_no]
                                         ,[factory_id]
                                         ,[factory_code]
                                         ,[enabled]
                                         ,[create_userid]
                                         ,[create_time]
                                         ,[update_userid]
                                         ,[update_time]
                                         ,[guid],rack_num,
                                            locate_id,locate_name,warehouse_id,warehouse_name,zone_id,zone_name,status,part_spec)
                                   VALUES (@rack_no,@corton_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num,
                                    @locate_id,@locate_name,@warehouse_id,@warehouse_name,@zone_id,@zone_name,@status,@part_spec
                                )");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@rack_no", destRackNo);//新台车号
                    updateStringparameters2.Add("@corton_no", sysStocks[0].PartNo);
                    updateStringparameters2.Add("@factory_id", wmsRackPackages[0].FactoryId);
                    updateStringparameters2.Add("@factory_code", wmsRackPackages[0].FactoryCode);
                    updateStringparameters2.Add("@enabled", "Y");
                    updateStringparameters2.Add("@create_userid", loginId);
                    updateStringparameters2.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters2.Add("@guid", Guid.NewGuid());
                    updateStringparameters2.Add("@rack_num", qty);
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters2.Add("@locate_id", wmsRackPackages[0].LocateId);
                    updateStringparameters2.Add("@locate_name", wmsRackPackages[0].LocateName);
                    updateStringparameters2.Add("@warehouse_id", wmsRackPackages[0].WarehouseId);
                    updateStringparameters2.Add("@warehouse_name", wmsRackPackages[0].WarehouseName);
                    updateStringparameters2.Add("@zone_id", wmsRackPackages[0].ZoneId);
                    updateStringparameters2.Add("@zone_name", wmsRackPackages[0].ZoneName);
                    updateStringparameters2.Add("@status", (int)WmsEnumUtil.StockStatus.INSTOCKED);
                    updateStringparameters2.Add("@part_spec", wmsRackPackages[0].PartSpec);

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);

                    updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"UPDATE [dbo].[wms_rack_package] SET rack_num=@rack_num where ruid=@ruid");
                    updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@rack_num", Convert.ToInt32(wmsRackPackages[0].RackNum) - Convert.ToInt32(qty));
                    updateStringparameters2.Add("@ruid", wmsRackPackages[0].Ruid);

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);

                    int recNum = repository.SQLTransaction(sqlStrings, parameterList);
                    if (recNum > 0)
                    {
                        List<WmsRackPackage> wmsMoves = repository.GetCarrierByName(destRackNo);

                        if (wmsMoves.Count > 0)
                        {
                            rfInfo.type = "PASS";
                            rfInfo.message = "拆分成功，请扫描源台车";
                            rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
                        }
                        else
                        {
                            rfInfo.type = "FAIL";
                            rfInfo.message = "未查询到数据，请检查！";
                        }
                    }
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "台车容量大于台车维护数量，请检查!";
                    return rfInfo;
                }
            }
            return rfInfo;
        }
    }
}
